<?php

namespace common\models\user;

use Yii;
use common\helpers\StringHelper;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%user_meta}}".
 *
 * @property int $id 用户ID
 * @property string $realname 真实姓名
 * @property string $mobile_hash 加密手机号
 * @property string $idcardno_hash 加密证件号
 * @property string $avatar 头像
 * @property int $gender 性别:0未知，1男，2女
 * @property string $email 邮箱
 * @property string $birthday 出生日期
 * @property string $mark 审核备注
 * @property int $ip 注册IP
 * @property int $company_id 用户隶属单位ID
 * @property int $created_at 注册时间
 * @property int $updated_at 更新时间
 */
class UserMeta extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user_meta}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'realname', 'mobile_hash', 'idcardno_hash'], 'required'],
            [['id', 'gender', 'ip', 'company_id', 'created_at', 'updated_at'], 'integer'],
            [['birthday'], 'safe'],
            [['realname'], 'string', 'max' => 50],
            [['mobile_hash', 'idcardno_hash', 'avatar', 'mark'], 'string', 'max' => 255],
            [['email'], 'string', 'max' => 100],
            [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '用户ID',
            'realname' => '真实姓名',
            'mobile_hash' => '加密手机号',
            'idcardno_hash' => '加密证件号',
            'avatar' => '头像',
            'gender' => '性别:0未知，1男，2女',
            'email' => '邮箱',
            'birthday' => '出生日期',
            'mark' => '审核备注',
            'ip' => '注册IP',
            'company_id' => '用户隶属单位ID',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * 关联账号信息
     */
    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'id']);
    }

    /**
     * 关联公司信息
     */
    public function getCompany() {
        return $this->hasOne(\common\models\company\Company::className(), ['id' => 'company_id']);
    }

    public function getUsername() {
        return $this->realname;
    }

    /**
     * 过滤手机号敏感字段
     * @inheritdoc
     */
    public function decryptMobile($mask = true) {
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        $mobile = $security->decryptMaster($this->mobile_hash);
        if ($mask !== false) {
            return StringHelper::maskChar($mobile, 3, 2);
        }
        return $mobile;
    }

    public function getMobile($mask = true) {
        return $this->decryptMobile($mask);
    }

    public function getIdcardno() {
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        return $security->decryptMaster($this->idcardno_hash);
    }

    public function getShieldData() {
        $data = [
            'password' => '123456',
        ];
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        return $security->encryptMaster(Json::encode($data));
    }

    /**
     * 融云TOKEN
     * @return 
     */
    public function getImToken() {
        return $this->hasOne(UserIm::className(), ['user_id' => 'id'])->andWhere([
            'or',
            ['expired_at' => 0],
            ['>', 'expired_at', time()]
        ]);
    }

}
